<template>
  <div class="shopcart" :class="{'activate': showShopcart}" @mouseover="showShopcart = true" @mouseout="showShopcart = false">
    <div class="shopcart-btn">
      <span @click="goToShopcart">采购单</span>
      <span class="badge" v-if="totalNum">（{{totalNum}}）</span>
    </div>
    <div class="shopcart-layout" v-if="showShopcart">
      <h4 class="title">最新加入的商品</h4>
      <ul class="goods-list">
        <li class="goods-wrapper" v-for="(item, index) in shopcart" :key="index">
          <div class="goods-img">
            <img :src="item.productCover" width="48" height="48">
          </div>
          <div class="goods-info">
            <div class="base-info">
              <p class="goods-name">{{item.productName}}</p>
              <span class="goods-price">¥{{item.sellPrice}}</span>
              <span class="count">x{{item.amount}}</span>
            </div>
            <div class="util-info">
              <span class="goods-unit">规格：{{item.attributeName}}</span>
            </div>
          </div>
          <span class="del-btn" @click="delItem(item)">删除</span>
        </li>
      </ul>
      <div class="shopcart-bottom">
        <span class="total">共{{totalNum}}件</span>
        <router-link target="_blank" class="check-btn" to="/order/shopcart">查看采购单</router-link>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions, mapState } from 'vuex'
export default {
  data () {
    return {
      showShopcart: false, // 是否显示采购单
      isLoading: false
    }
  },
  mounted () {
    this.init()
  },
  methods: {
    ...mapActions({
      getShoppingList: 'shopcart/getShopcart', // 获取采购单信息
      delShopcart: 'shopcart/delShopcart' // 删除采购单商品
    }),
    // 初始化
    init () {
      this.getShoppingList()
    },
    // // 删除商品
    delItem (item) {
      if (!this.isLoading) {
        this.isLoading = true
        let param = {
          cartAttributeIds: item.cartAttributeIds
        }
        this.delShopcart(param).then(_ => {
          this.isLoading = false
        }).catch(err => {
          console.error(err)
          this.isLoading = false
        })
      }
    },
    // 跳转到采购单页面
    goToShopcart () {
      this.$router.push({
        path: '/order/shopcart'
      })
    }
  },
  computed: {
    ...mapState({
      'shopcart': state => state.shopcart.shoppingList
    }),
    totalNum () {
      let num = 0
      this.shopcart.forEach(item => {
        num += item.amount
      })
      return num
    }
  }
}
</script>

<style lang="scss" scoped>
@import '@/assets/scss/basic.scss';
.shopcart {
  position: relative;
  z-index: 101;
  .shopcart-btn{
    position: relative;
    z-index: 110;
    width: 100%;
    height: 35px;
    line-height: 35px;
    text-align: center;
    cursor: pointer;
  }
  &.activate .shopcart-btn {
    color: #434343;
    font-weight: 500;
    background: #ffffff;
  }
  .shopcart-layout {
    position: absolute;
    z-index: 102;
    top: 35px;
    right: 0%;
    width: 340px;
    color: $f_main;
    background: #ffffff;
    box-shadow: 0 1px 6px rgba(128, 128, 128, 0.3);
    .title {
      padding: 10px 0 3px 10px;
      line-height: 1;
      font-size: $fz_14;
    }
    .goods-list {
      max-height: 535px;
      overflow-x: hidden;
      overflow-y: auto;
      .goods-wrapper {
        position: relative;
        padding: 8px 10px;
        line-height: 1;
        border-bottom: dashed 1px $border_color;
        overflow: hidden;
        &:last-child {
          border: none;
        }
        .goods-img {
          float: left;
          width: 50px;
          height: 50px;
          border: solid 1px $border_color;
        }
        .goods-info {
          float: right;
          margin-left: 10px;
          width: calc(100% - 60px);
          .base-info {
            padding-top: 8px;
            font-size: 0;
            .goods-name {
              display: inline-block;
              vertical-align: middle;
              font-size: $fz_14;
              width: 160px;
              white-space: nowrap;
              text-overflow: ellipsis;
              overflow: hidden;
            }
            .goods-price {
              display: inline-block;
              vertical-align: middle;
              font-size: $fz_14;
              width: 70px;
              text-align: center;
              color: $red;
            }
            .count {
              display: inline-block;
              vertical-align: middle;
              font-size: $fz_12;
              width: 30px;
              text-align: right;
              color: $f_normal;
            }
          }
          .util-info {
            margin-top: 10px;
            .goods-unit {
              font-size: $fz_12;
              color: $f_sub;
            }
          }
        }
        .del-btn {
          position: absolute;
          bottom: 8px;
          right: 10px;
          padding: 4px;
          font-size: $fz_12;
          color: $f_main;
          font-weight: 400;
          border: solid 1px $border_color;
          border-radius: 4px;
          cursor: pointer;
        }
      }
    }
    .shopcart-bottom {
      margin-top: 10px;
      padding: 0 10px;
      line-height: 35px;
      height: 35px;
      text-align: right;
      background: $bg_light;
      border-top: solid 1px $border_color;
      .total {
        font-size: $fz_12;
      }
      .check-btn {
        margin-left: 10px;
        padding: 2px 4px;
        font-size: $fz_12;
        font-weight: 200;
        color: #ffffff;
        background: $green;
        border-radius: 4px;
        cursor: pointer;
        &:hover {
          background: $green_l;
        }
      }
    }
  }
}
</style>
